package com.macrohard.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macrohard.admin.service.AnnounceService;
import com.macrohard.common.base.response.R;
import com.macrohard.mapper.domain.Announce;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("announce")
@Validated
@Api(tags = "公告接口")
@RequiredArgsConstructor
public class AnnounceController {

	private final AnnounceService announceService;

	@GetMapping
	@ApiOperation("分页获取所有公告")
	public R get(@NotNull Integer current, @NotNull Integer pageSize) {
		Page<Announce> page = new Page<>(current, pageSize);
		LambdaQueryWrapper<Announce> queryWrapper = new LambdaQueryWrapper<>();
		page = announceService.page(page, queryWrapper);
		return R.ok("ok", page);
	}

	@ApiOperation("根据id获取公告")
	@GetMapping("{id}")
	public R get(@PathVariable Integer id) {
		return R.ok("ok", announceService.getById(id));
	}

	@ApiOperation("添加公告")
	@PostMapping
	public R add(@RequestBody Announce announce) {
		return R.wrapEffectResult(announceService.save(announce));
	}

	@PutMapping
	@ApiOperation("修改公告")
	public R update(@RequestBody Announce announce) {
		return R.wrapEffectResult(announceService.updateById(announce));
	}

	@ApiOperation("根据id删除公告")
	@DeleteMapping("{id}")
	public R delete(@PathVariable Integer id) {
		return R.wrapEffectResult(announceService.removeById(id));
	}

	@ApiOperation("批量删除公告")
	@DeleteMapping
	public R delete(@RequestBody List<Integer> ids) {
		return R.wrapEffectResult(announceService.removeByIds(ids));
	}
}
